/* Check blaum_roth_coding_bitmatrix() */
#include <stdio.h>
#include "cauchy.h"
#include "jerasure.h"

int main( int argc, char** argv )
{
	int *old_matrix;
	int m = atoi( argv[1] );
	int k = atoi( argv[2] );
	int w = atoi( argv[3] );
	int* X;
	int* Y;
	int i, j, tmp, fsize;

	fsize = 1 << w;

	if( w < m + k ) return 1;

	X = (int*) malloc( sizeof(int) * m );
	Y = (int*) malloc( sizeof(int) * k );
	srand48(m);
	fprintf( stdout, "X = " );
	for( i = 0; i < m; i++ )
	{
		tmp = lrand48() % fsize;
		for( j = 0; j < i && X[j] != tmp; j++ );
		if( j == i )
		{
			X[i] = tmp;
			fprintf( stdout, "%d ", X[i] );
		}
		else
			i--;
	}
	fprintf( stdout, "\n" );
	fprintf( stdout, "Y = " );
	for( i = 0; i < k; i++ )
	{
		tmp = lrand48() % fsize;
		for( j = 0; j < m && X[j] != tmp; j++ );
		if( j == m )
		{
			for( j = 0; j < i && Y[j] != tmp; j++ );
			if( j == i )
			{
				Y[i] = tmp;
				fprintf( stdout, "%d ", Y[i] );
			}
			else
				i--;
		}
		else i--;
	}
	fprintf( stdout, "\n" );
	
	old_matrix = cauchy_xy_coding_matrix( k, m, w, X, Y );
	if( old_matrix != NULL )
		jerasure_print_matrix( old_matrix, m, k, w );

	return 0;
}
